#include "AddressBook.h"
Person** AddressBook::getPersonList(){
    return this->personList;
}
AddressBook::AddressBook(int maxSize){
    this->personList = new Person *[maxSize];
    this->length = 0;
    this->maxSize = maxSize;
}

AddressBook::~AddressBook(){
    delete personList;
}

int AddressBook::getLength() const {
    return this->length;
}

int AddressBook::getMaxSize() const {
    return this->maxSize;
}

void AddressBook::addPerson(Person *person) {
    this->personList[this->length] = person;
    this->length++;
}

bool AddressBook::isEmpty() const {
    return this->length == 0;
}

bool AddressBook::isFull() const {
    return (this->length == this->maxSize);
}

string AddressBook::deletePerson(string name,long phone) {
    int index;
    if(name == "") index = indexOf(phone);
    else if(phone == -1) index = indexOf(name);
    else index = indexOf(name, phone);

    if(index == -1) return "Not found!";
    int p = index+1;
    while(p < length){
        swap(this->personList[index], this->personList[p]);
        index++;
        p++;
    }
    this->length--;
    return "Delete success!";
}

int AddressBook::indexOf(long phone) {
    for (int i = 0; i < this->length; i++) {
        if (this->personList[i]->getPhone() == phone) {
            return i;
        }
    }
    return -1;
}

int AddressBook::indexOf(string name) {
    for (int i = 0; i < this->length; i++) {
        if (this->personList[i]->getName() == name) {
            return i;
        }
    }
    return -1;
}

int AddressBook::indexOf(string name,long phone) {
    for (int i = 0; i < this->length; i++) {
        if (this->personList[i]->getName() == name && this->personList[i]->getPhone() == phone) {
            return i;
        }
    }
    return -1;
}